package com.fancon.rage.comic.persistence.appengine;

import java.util.Collection;

import javax.jdo.PersistenceManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jdo.JdoTemplate;
import org.springframework.stereotype.Repository;

import com.fancon.rage.comic.domain.Greeting;
import com.fancon.rage.comic.domain.Image;
import com.fancon.rage.comic.domain.ImageRepository;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
@Repository
public class JdoImageRepository implements ImageRepository{

	private JdoTemplate jdoTemplate;
	@Autowired
	public JdoImageRepository(PersistenceManagerFactory pmf) {
		this.jdoTemplate = new JdoTemplate(pmf);
	}
	@Override
	public boolean doStuff() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void save(Image img) {
		// TODO Auto-generated method stub
		jdoTemplate.makePersistent(img);	
	}

	@Override
	public Collection<Image> findAll() {
		// TODO Auto-generated method stub

		Collection<Image> imgs = jdoTemplate.find(Image.class);
		System.out.println("greetings count:" + imgs.size());
		
		return imgs;
	}

	@Override
	public Image get(long id) {
		// TODO Auto-generated method stub
		Key key = KeyFactory.createKey(Image.class.getSimpleName(), id);
		Image img = jdoTemplate.getObjectById(Image.class, key);
		
		return img;
	}

}
